package com.supersit.GZFDA.activity;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.supersit.GZFDA.R;
import com.supersit.GZFDA.api.APIConstants;
import com.supersit.GZFDA.util.AppManager;
import com.zhy.autolayout.AutoLayoutActivity;

public class UpdatePwd extends AutoLayoutActivity {
	private Button btn_sub;
	private EditText oldPwd, newPwd, r_newpWD;
	private ImageView ivBack;
	private ProgressDialog pd;
	private Context context;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.update_password);
		AppManager.getAppManager().addActivity(UpdatePwd.this);
		context = this;
		initView();
		listener();
	}

	public void initView() {
		btn_sub = (Button) findViewById(R.id.btn_update_pwd);
		oldPwd = (EditText) findViewById(R.id.et_oldpwd);
		newPwd = (EditText) findViewById(R.id.et_newpwd);
		ivBack = (ImageView) findViewById(R.id.iv_back);
		r_newpWD = (EditText) findViewById(R.id.et_r_newpwd);
	}

	private void listener() {
		btn_sub.setOnClickListener(onClickListener);
		ivBack.setOnClickListener(onClickListener);
	}

	private OnClickListener onClickListener = new OnClickListener() {

		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			switch (arg0.getId()) {
			case R.id.btn_update_pwd:
				if (TextUtils.isEmpty(oldPwd.getText().toString())) {
					Toast.makeText(context, "密码不能为空", 1).show();
					return;
				}
				if (TextUtils.isEmpty(newPwd.getText().toString())) {
					Toast.makeText(context, "新密码不能为空", 1).show();
				}
				if (TextUtils.isEmpty(r_newpWD.getText().toString())) {
					Toast.makeText(context, "确认密码不能为空", Toast.LENGTH_SHORT)
							.show();
					return;
				}
				if (r_newpWD.getText().toString().trim()
						.equals(newPwd.getText().toString().trim())) {
					updatePwd(oldPwd.getText().toString(), newPwd.getText()
							.toString());
				} else {
					Toast.makeText(context, "两次输入密码不一致", 1).show();
				}
				break;
			case R.id.iv_back:
				finish();
				break;
			}
		}
	};

	private void updatePwd(String oldPwd, final String newPwd) {
		String url = APIConstants.updatePwd;
		SharedPreferences sp = getSharedPreferences("userInfo",
				Context.MODE_PRIVATE);
		AsyncHttpClient client = new AsyncHttpClient();
		RequestParams params = new RequestParams();
		String account = sp.getString("USER_NAME", "");
		params.put("account", account);
		params.put("password", oldPwd);
		params.put("newpassword", newPwd);
		Log.d("tag", "updatePWD:" + url + "   " + params.toString());
		client.get(url, params, new AsyncHttpResponseHandler() {

			@Override
			public void onFailure(int arg0, Header[] arg1, byte[] arg2,
					Throwable arg3) {
				// TODO Auto-generated method stub
				super.onFailure(arg0, arg1, arg2, arg3);
				Log.d("tag", "Failure：" // + new String(arg2) + "~~~~"
						+ arg3.getMessage());
				pd.dismiss();
				Toast.makeText(context, "服务器异常,密码修改失败", Toast.LENGTH_SHORT)
						.show();
			}

			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				super.onStart();
				pd = ProgressDialog
						.show(UpdatePwd.this, "修改密码", "....正在提交....");
			}

			@Override
			public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
				// TODO Auto-generated method stub
				super.onSuccess(arg0, arg1, arg2);
				pd.dismiss();
				Log.d("tag", "Success:" + new String(arg2));
				JSONObject json = null;
				try {
					json = new JSONObject(new String(arg2));
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				if ("0".equals(json.optString("flag"))) {
					Toast.makeText(context, "密码修改成功", Toast.LENGTH_SHORT)
							.show();
					SharedPreferences.Editor editor = getSharedPreferences(
							"userInfo", Context.MODE_WORLD_READABLE).edit();
					editor.putString("PASSWORD", newPwd);
					editor.commit();
					AppManager.getAppManager().finishActivity();
				} else {
					Toast.makeText(context, "密码修改失败", Toast.LENGTH_SHORT)
							.show();
					UpdatePwd.this.oldPwd.setText("");
					UpdatePwd.this.newPwd.setText("");
				}
			}

		});

	}
}
